package com.loe.imageplayer;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.util.Pair;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class PhotoDialog extends DialogFragment
{
    private View rootView;
    private PhotoViewPager viewPager;
    private TextView textName;
    private TextView textIndex;
    private View viewTitle;
    private View buttonClose;

    private List<String> urlList = new ArrayList<>();
    private List<String> nameList = new ArrayList<>();

    private boolean isShowIndex = false;
    private int index = 0;
    private int paddingHorizontal = px(10);

    public void show(FragmentManager manager)
    {
        super.show(manager, "photo");
        Log.d("runtime", "show()");
    }

    public void show(Activity activity)
    {
        if(activity instanceof FragmentActivity)
        {
            show(((FragmentActivity) activity).getSupportFragmentManager());
        }
    }

    public void show(Fragment fragment)
    {
        show(fragment.getChildFragmentManager());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        if (rootView == null)
        {
            rootView = inflater.inflate(R.layout.photo_dialog, container);
        }

        return rootView;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
    {
        super.onViewCreated(view, savedInstanceState);
        Log.d("runtime", "onViewCreated()");

        viewPager = rootView.findViewById(R.id.viewPager);
        textName = rootView.findViewById(R.id.textName);
        textIndex = rootView.findViewById(R.id.textIndex);
        viewTitle = rootView.findViewById(R.id.viewTitle);
        buttonClose = rootView.findViewById(R.id.buttonClose);
    }


    @Override
    public void onStart()
    {
        super.onStart();
        Log.d("runtime", "onStart()");

        Window window = getDialog().getWindow();
        window.setWindowAnimations(R.style.dialogImageAnimation);
        window.setBackgroundDrawableResource(android.R.color.transparent);
        window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setGravity(Gravity.CENTER);

        View root = window.getDecorView();
        root.setPadding(paddingHorizontal, 0, paddingHorizontal, 0);

        // ****************************** 显示逻辑 ******************************

        if(index >= urlList.size()) index = urlList.size() -1;

        textIndex.setVisibility(urlList.size() > 1 && isShowIndex ? View.VISIBLE : View.GONE);

        textName.setText(nameList.get(index));
        textIndex.setText((index + 1) + "/" + urlList.size());

        viewPager.init(urlList, index, getChildFragmentManager());

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener()
        {
            @Override
            public void onPageSelected(int i)
            {
                textName.setText(nameList.get(i));
                textIndex.setText((i + 1) + "/" + urlList.size());
            }

            @Override
            public void onPageScrolled(int i, float v, int i1)
            {
            }

            @Override
            public void onPageScrollStateChanged(int i)
            {
            }
        });

        viewTitle.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dismiss();
            }
        });

        buttonClose.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                dismiss();
            }
        });
    }

    @Override
    public void onCancel(DialogInterface dialog)
    {
        super.onCancel(dialog);
        Log.d("runtime", "onCancel()");
    }

    @Override
    public void onDismiss(DialogInterface dialog)
    {
        super.onDismiss(dialog);
        Log.d("runtime", "onDismiss()");
    }

    /************************************** 配置 *************************************/

    public PhotoDialog clear()
    {
        urlList.clear();
        nameList.clear();
        return this;
    }

    public PhotoDialog paddingHorizontal(int dp)
    {
        paddingHorizontal = px(dp);
        return this;
    }

    public PhotoDialog isShowIndex()
    {
        isShowIndex = true;
        return this;
    }

    public PhotoDialog index(int index)
    {
        this.index = index;
        return this;
    }

    public PhotoDialog add(String name, String url)
    {
        urlList.add(url);
        nameList.add(name);
        return this;
    }

    public PhotoDialog add(String url)
    {
        add("", url);
        return this;
    }

    public PhotoDialog addList(List<Pair<String, String>> list)
    {
        for (Pair<String, String> pair : list)
        {
            add(pair.first, pair.second);
        }
        return this;
    }

    public PhotoDialog addUrls(List<String> urlList)
    {
        for (String url : urlList)
        {
            add(url);
        }
        return this;
    }

    public PhotoDialog addJsonArray(JSONArray js, String nameKey, String urlKey)
    {
        for (int i = 0; i < js.length(); i++)
        {
            JSONObject json = js.optJSONObject(i);
            add(json.optString(nameKey), json.optString(urlKey));
        }
        return this;
    }

    public PhotoDialog addJsonArray(JSONArray js)
    {
        addJsonArray(js, "name", "url");
        return this;
    }

    private int px(double dp)
    {
        return (int) (dp * Resources.getSystem().getDisplayMetrics().density + 0.5);
    }
}